package com.springboot.framework.response;

import java.util.HashMap;
import java.util.Map;

public class ResponseMap<M> extends HashMap {

    private Map data = new HashMap();

    private int i = 0;

    public ResponseMap<M> ok(){
        return this.ok(null);
    }

    public ResponseMap<M> ok(M data){
        this.put("code",HttpCode.OK.getCode());
        this.put("message",HttpCode.OK.getMessage());
        this.put("msg",HttpCode.OK.getMessage());

        if(data != null){
            /*第一次 i=0 */
            if(i == 0){
                i++;
                this.put("data",data);
            }else{
            /*第二次以后*/
                /* 第二次 i=1 */
                if(this.data.isEmpty()) {
                    this.data.put("data" + i, this.get("data"));
                }
                i++;
                this.data.put("data"+i,data);
                this.put("data",this.data);
            }
        }
        return this;
    }

    public ResponseMap<M> fail(String message){
        this.put("code",HttpCode.FAIL.getCode());
        this.put("message",message);
        this.put("msg",message);
        return this;
    }

    public ResponseMap addData(String key,M data){
        if(data != null){
            if(data != null){
                /*第一次 i=0 */
                if(i == 0){
                    i++;
                    this.put("data",data);
                }else{
                    /*第二次以后*/
                    /* 第二次 i=1 */
                    if(this.data.isEmpty()) {
                        this.data.put("data" + i, this.get("data"));
                        i++;
                    }
                    this.data.put(key,data);
                    this.put("data",this.data);
                }
            }
        }
        return this;
    }

}
